<?php

namespace App\Http\Controllers\Admin\Member\Service;

use App\ComboRefundLog;
use App\Http\Controllers\Admin\Member\Repository\Member as MemberRepository;
use Carbon\Carbon;
use Illuminate\Http\Request;

class Member
{
    /**
     * 发布状态 1启用 2禁用
     */
    const ON = 1;
    const OFF = 2;
    /**
     * 性别
     */
    const MEN   = 1;
    const WOMEN = 2;
    const SECRET = 0;
    /**
     *订阅状态
     */
    const SUBSCRIBE = 1;
    const NOT_SUBSCRIBE = 0;

    /**
     * 角色
     */
    const ROLE_VISITOR = 1;
    const ROLE_TRAVEL = 2;

    /**
     * Http请求
     * @var $request
     */
    private $request;
    private $repository;

    public function __construct ()
    {
        $this->repository = new MemberRepository();
    }
    //常用参数操作
    /**
     * @param $status
     *
     * @return mixed
     */
    public function getStatusString($status){
        $statusList =  $this->getStatusList();
        return $statusList[$status];
    }


    /**
     * @return array
     */
    public static function getStatusList(){
        return [
            self::ON =>  '启用',
            self::OFF =>  '禁用'
        ];
    }

    public function getRoleString($status){
        $statusList =  $this->getRoleList();
        return $statusList[$status];
    }


    /**
     * @return array
     */
    public static function getRoleList(){
        return [
            self::ON =>  '游客',
            self::OFF =>  '旅行社'
        ];
    }

    /**
     * @param $status
     *
     * @return mixed
     */
    public function getSexString($status){
        $statusList =  $this->getSexList();
        return $statusList[$status];
    }


    /**
     * @return array
     */
    public static function getSexList(){
        return [
            self::MEN =>  '男',
            self::WOMEN =>  '女',
            self::SECRET    =>  '保密'
        ];
    }


    /**
     * @param $status
     *
     * @return mixed
     */
    public function getSubscribeString($status){
        $statusList =  $this->getSubscribeList();
        return $statusList[$status];
    }


    /**
     * @return array
     */
    public static function getSubscribeList(){
        return [
            self::SUBSCRIBE =>  '已订阅',
            self::NOT_SUBSCRIBE =>  '未订阅',
        ];
    }
    //读操作



    public function getMember($id){
        return $this->repository->with(['parent'])->find($id);
    }
    public function getMemberPath($id){
        $data= $this->repository->whereRaw("FIND_IN_SET({$id},path)")->where('id','<>',$id)->get()->toArray();
        $dt = Carbon::now();
        $year=$dt->year;
        $months=$dt->month;
        $month=Carbon::createFromDate((string)$year, (string)$months, '1');
        $lastMonth=Carbon::createFromDate((string)$year, (string)($months+1), '1');
        foreach ($data as $k=>&$v){
            $v['refund_money']=ComboRefundLog::where('member_id',$v['id'])->whereBetween('created_at',[$month,$lastMonth])->sum('refund_money');
        }
        return $data;

    }
    public function getSumPath($id,$wallet='wallet_all_push_money'){
        $son = $this->repository->where('pid',$id)->sum($wallet);
        $grandSon =  $this->repository->whereRaw("FIND_IN_SET({$id},path)")->where('id','<>',$id)->where('pid','<>',$id)->sum($wallet);
        return $son+$grandSon;
    }
        /**
     * @param Request $request
     *
     * @return $this|\Illuminate\Database\Eloquent\Builder|static
     */
    public function getPageList(Request $request,$order=false){
        $this->request = $request;
        $model=$this->repository->with(['parent']);
        //关键字
        if(isset($this->request->searchKey)){
            $model=$model->where('nickname','like', '%'.$this->request->searchKey.'%');
        }
        if(isset($this->request->searchMobile)){
            $model=$model->where('mobile',$this->request->searchMobile);
        }
        //状态
        if(isset($this->request->status)&&$this->request->status!=''){
            $model=$model->where('status','=',$this->request->status);
        }
        if(isset($this->request->sex)&&$this->request->sex!=''){
            $model=$model->where('sex','=',$this->request->sex);
        }
        if(isset($this->request->role)&&$this->request->role!=''){
            $model=$model->where('role','=',$this->request->role);
        }
        if(isset($this->request->subscribe)&&$this->request->subscribe!=''){
            $model=$model->where('subscribe','=',$this->request->subscribe);
        }

        if(isset($this->request->parent_name)&&$this->request->parent_name!=''){
           $member= \App\Http\Controllers\Admin\Member\Repository\Member::where('nickname',$this->request->parent_name)->first();
           $model=$model->where('pid',$member->id);
        }
        //时间
        if(isset($this->request->searchTime)&&$this->request->searchTime!=''){
            $timeArray=explode('~',$this->request->searchTime);
            $start_time=trim($timeArray[0]);
            $end_time=trim($timeArray[1]);
            $model=$model->whereBetween('created_at',[$start_time,$end_time]);
        }
        if($order==true){
            $model = $model->orderBy('sort','ASC');
        }
        $model=$model->paginate($this->request->limit);

        if($model){
            $model=$model->toArray();
        }
        $dt = Carbon::now();
        $year=$dt->year;
        $months=$dt->month;
        $month=Carbon::createFromDate((string)$year, (string)$months, '1');
       $lastMonth=Carbon::createFromDate((string)$year, (string)($months+1), '1');
        foreach ($model['data'] as $k=>&$v){
            $refund_money=ComboRefundLog::where('member_id',$v['id'])->whereBetween('created_at',[$month,$lastMonth])->sum('refund_money');
            $v['status_name'] = self::getStatusList()[$v['status']];
            $v['parent_name'] = isset($v['parent'])?$v['parent']['nickname']:'无推广人';
            $v['sex_name']  =   self::getSexString($v['sex']);
            $v['role_name'] = self::getRoleString($v['role']);
            $v['refund_money']=$refund_money;
            //$v['subscribe_name'] =   self::getSubscribeString($v['subscribe']);
        }
        return $model;
    }

    //写操作
    /**
     * 禁用
     * @param array $id
     */
    public function draft($id = []){
        if($id == []){
            $this->repository->where('status',self::ON)->update(['status'=>self::OFF]);
        }else{
            if(is_array($id)){
                $this->repository->whereIn('id',$id)->where('status',self::ON)->update(['status'=>self::OFF]);
            }else{
                $this->repository->where('id',$id)->where('status',self::ON)->update(['status'=>self::OFF]);

            }
        }
    }

    /**
     * 启用
     * @param array $id
     */
    public function published($id = []){
        if($id == []){
            $this->repository->where('status',self::OFF)->update(['status'=>self::ON]);
        }else{
            if(is_array($id)){
                $this->repository->whereIn('id',$id)->where('status',self::OFF)->update(['status'=>self::ON]);
            }else{
                $this->repository->where('id',$id)->where('status',self::OFF)->update(['status'=>self::ON]);

            }
        }
    }
    public function visitor($id = []){
        if($id == []){
            $this->repository->where('role',self::ROLE_TRAVEL)->update(['role'=>self::ROLE_VISITOR]);
        }else{
            if(is_array($id)){
                $this->repository->whereIn('id',$id)->where('role',self::ROLE_TRAVEL)->update(['role'=>self::ROLE_VISITOR]);
            }else{
                $this->repository->where('id',$id)->where('role',self::ROLE_TRAVEL)->update(['role'=>self::ROLE_VISITOR]);

            }
        }
    }
    public function travel($id = []){
        if($id == []){
            $this->repository->where('role',self::ROLE_VISITOR)->where('pid',0)->update(['role'=>self::ROLE_TRAVEL]);
        }else{
            if(is_array($id)){
                $this->repository->whereIn('id',$id)->where('role',self::ROLE_VISITOR)->update(['role'=>self::ROLE_TRAVEL]);
            }else{
                $this->repository->where('id',$id)->where('role',self::ROLE_VISITOR)->update(['role'=>self::ROLE_TRAVEL]);

            }
        }
    }

    public function sortCol($id = [],$sort = 50){
        if($id == []){
        }else{
            if(is_array($id)){
                $this->repository->whereIn('id',$id)->where('sort','<>',$sort)->update(['sort'=>$sort]);
            }else{
                $this->repository->where('id',$id)->where('sort','<>',$sort)->update(['sort'=>$sort]);

            }
        }
    }
    public function exportList(Request $request){
        $this->request = $request;
        $model=$this->repository->with(['parent']);
        //关键字
        if(isset($this->request->searchKey)){
            $model=$model->where('nickname','like', '%'.$this->request->searchKey.'%');
        }
        if(isset($this->request->searchMobile)){
            $model=$model->where('mobile',$this->request->searchMobile);
        }
        //状态
        if(isset($this->request->status)&&$this->request->status!=''){
            $model=$model->where('status','=',$this->request->status);
        }
        if(isset($this->request->sex)&&$this->request->sex!=''){
            $model=$model->where('sex','=',$this->request->sex);
        }
        if(isset($this->request->role)&&$this->request->role!=''){
            $model=$model->where('role','=',$this->request->role);
        }
        if(isset($this->request->subscribe)&&$this->request->subscribe!=''){
            $model=$model->where('subscribe','=',$this->request->subscribe);
        }

        if(isset($this->request->parent_name)&&$this->request->parent_name!=''){
            $member= \App\Http\Controllers\Admin\Member\Repository\Member::where('nickname',$this->request->parent_name)->first();
            $model=$model->where('pid',$member->id);
        }
        //时间
        if(isset($this->request->searchTime)&&$this->request->searchTime!=''){
            $timeArray=explode('~',$this->request->searchTime);
            $start_time=trim($timeArray[0]);
            $end_time=trim($timeArray[1]);
            $model=$model->whereBetween('created_at',[$start_time,$end_time]);
        }
        $model=$model->get();
        if($model){
            $model=$model->toArray();
        }
        $cellData = [
            [
                'id',
                '状态',
                '推广人',
                '身份',
                '手机号',
                '性别',
                '累计提成',
                '已使用提成',
                '提成',
                '本月提成',
                '使用次数',
                '注册时间',
            ]
        ];
        $dt = Carbon::now();
        $year=$dt->year;
        $months=$dt->month;
        $month=Carbon::createFromDate((string)$year, (string)$months, '1');
        $lastMonth=Carbon::createFromDate((string)$year, (string)($months+1), '1');
        foreach ($model as $k=>&$v) {
            $temp_data = [
                $v['id'],
                $v['status_name'] = self::getStatusList()[$v['status']],
                $v['parent_name'] = isset($v['parent'])?$v['parent']['nickname']:'无推广人',
                $v['role_name'] = self::getRoleString($v['role']),
                $v['mobile'],
                $v['sex_name']  =   self::getSexString($v['sex']),
                $v['wallet_all_push_money'],
                $v['wallet_used_push_money'],
                $v['wallet_push_money'],
                $v['refund_money']=ComboRefundLog::where('member_id',$v['id'])->whereBetween('created_at',[$month,$lastMonth])->sum('refund_money'),
                $v['wallet_spots'],
                $v['created_at'],

            ];
            $cellData[] = $temp_data;
        }
        return $cellData;
    }
    /**
     * @param array $id
     */
    public function del($id = []){
            if($id == []){

            }else{
                if(is_array($id)){
                    $this->repository->whereIn('id',$id)->delete();
                }else{
                    $this->repository->where('id',$id)->delete();

                }
            }
    }
}
